package com.indusfo.spc.pda.service.impl;

import com.indusfo.spc.pda.mapper.OrderDosingMapper;
import com.indusfo.spc.pda.pojo.OrderAndMatDto;
import com.indusfo.spc.pda.pojo.OrderDosing;
import com.indusfo.spc.pda.service.OrderDosingService;
import com.indusfo.spc.pojo.Order;
import com.indusfo.spc.untlis.DateUtils;
import com.indusfo.spc.untlis.StringUtils;
import com.indusfo.spc.vo.JSONObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

/**
 * @ClassName OrderDosingServiceImpl
 * @Description
 * @Author louk
 * @Date 2019/12/24 21:44
 */
@Service
public class OrderDosingServiceImpl implements OrderDosingService {

    private static final Logger logger= LoggerFactory.getLogger(OrderDosingServiceImpl.class);

    @Resource
    OrderDosingMapper orderDosingMapper;
    @Override
    public JSONObject queryOrderAndMat(Order order) {
        String checkResult = checkParam(order);
        if(!StringUtils.isEmpty(checkResult)){
            return JSONObject.build(JSONObject.ERROR,checkResult);
        }
        List<OrderAndMatDto> orderAndMatDtoList = orderDosingMapper.queryOrderAndMat(order);
        if(!orderAndMatDtoList.isEmpty()){
            int count=orderDosingMapper.countQueryOrderAndMat(order);
            if(count>0){
                return JSONObject.oK("查询成功",orderAndMatDtoList,count);
            }
        }
        return JSONObject.oK("暂无相关数据");
    }

    @Override
    public JSONObject queryOrderDosing(OrderDosing orderDosing) {
        Integer orderId = orderDosing.getOrderId();
        if(orderId==null || "".equals(orderId)){
            return JSONObject.build(JSONObject.ERROR,"未获取到任务单信息");
        }
        List<OrderDosing> orderDosingList= orderDosingMapper.queryOrderDosingList(orderDosing);
        if(!orderDosingList.isEmpty()){
           int count= orderDosingMapper.countQueryOrderDosingList(orderDosing);
           return JSONObject.oK("查询成功",orderDosingList,count);
        }
        return JSONObject.oK("暂无相关数据");
    }

    public String checkParam(Order order){
        //任务单id
        Integer productlineId = order.getProductlineId();
        //发布时间
        String startDate = order.getStartDate();
        if(StringUtils.isEmpty(startDate)){
           return "未获取到日期";
        }else{
            try{
                Date date = DateUtils.dateTime(DateUtils.YYYY_MM_DD, startDate);
            }catch (Exception e){
                return "日期格式错误";
            }
        }
        if(productlineId==null || "".equals(productlineId)){
            return "未获取到产线信息";
        }
        return null;
    }
}
